package com.wxc.thread;

import com.wxc.common.User;
import com.wxc.util.ConnectionUtil;
import com.wxc.util.InputUtil;
import com.wxc.util.SocketUtil;
import com.wxc.vo.Message;
import com.wxc.vo.Resp;
import com.wxc.vo.RespCode;

import java.net.Socket;
import java.util.Scanner;

/**
 * @author wxc
 * @date 2021年10月04日 21:41
 */
public class SendThread extends Thread {
    private final ConnectionUtil connectionUtil = new ConnectionUtil();
    private final Socket socket;
    private final User user;
    private ReceiveThread receiveThread;

    public SendThread(Socket socket, User user) {
        this.socket = socket;
        this.user = user;
    }

    @Override
    public void run() {
        if(!login()) return;
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("=========二级菜单-用户:" + user.getUserName() + "=========");
            System.out.println("\t\t 1 显示在线用户列表");
            System.out.println("\t\t 2 群发消息");
            System.out.println("\t\t 3 私聊消息");
            System.out.println("\t\t 4 发送文件");
            System.out.println("\t\t 9 退出系统");
            System.out.println("请输入您的选择: ");
            String key = scanner.nextLine().trim();
            switch (key) {
                case "1":
                    showUserNames();
                    break;
                case "2":
                    chat(InputUtil.chatAll(user.getUserName()));
                    break;
                case "3":
                    chat(InputUtil.chat(user.getUserName()));
                    break;
                case "4":
                    sendFile(InputUtil.sendFile(user.getUserName()));
                    break;
                case "9":
                    quit();
                    loop = false;
                    break;
            }
        }
    }

    private void sendFile(Message<Object> message) {
        if(message == null) return;
        connectionUtil.send(socket, message);
    }

    private void chat(Message<Object> message) {
        connectionUtil.send(socket, message);
    }

    private void quit() {
        Message<String> sendMessage = new Message<>();
        sendMessage.setType(Message.MessageType.QUIT);
        connectionUtil.send(socket, sendMessage);

    }

    private boolean login() {

        Message<User> sendMessage = new Message<>();
        sendMessage.setType(Message.MessageType.LOGIN);
        sendMessage.setData(user);
        // send Authentication request
        connectionUtil.send(socket, sendMessage);
        // receive
        Message<Resp<String>> message = connectionUtil.receive(socket);
        if (message.getData().getRespCode() == RespCode.SUCCESS) {
            user.setLoginStatus(true);
            receiveThread = new ReceiveThread(socket, user);
            receiveThread.start();
            return true;
        }
        // fail
        System.out.println(message.getData().getRespCode());
        SocketUtil.close(socket);
        return false;
    }

    private void showUserNames() {
        Message<String> sendMessage = new Message<>();
        sendMessage.setType(Message.MessageType.SHOW_USERS);
        connectionUtil.send(socket, sendMessage);
    }
}
